﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Common;

namespace QuanLyBanHang
{
    /// <summary>
    /// Phan quyen
    /// </summary>
    /// <author>Viet sang</author>
    /// <date>28/10/2011</date>
    public partial class frmPhanQuyenNhom : frmManager
    {

        #region Constructors

        public frmPhanQuyenNhom()
        {
            InitializeComponent();
            pnlChiTiet.Enabled = true;
            tsbCancel.Visible = false;
            tsbSua.Visible = false;
            tsbLuu.Visible = false;

            cbxNhomNguoiDung.SelectedValueChanged += new EventHandler(cbxNhomNguoiDung_SelectedValueChanged);
        }

        #endregion

        #region Protected methods

        protected override void InitForm()
        {
            _bindingSource.DataSource = typeof(ACL_PhanQuyenNhomNguoiDung);

        }

        protected override void LoadData()
        {
            this._bindingSource.DataSource = Cache.Context.ACL_PhanQuyenNhomNguoiDung.OrderBy(s => s.IDNhomNguoiDung);
            this.dgvPhanQuyen.DataSource = _bindingSource;

            base.LoadData();
        }

        protected override void AddDataBinding()
        {
            cbxNhomNguoiDung.DataSource = Cache.Context.ACL_NhomNguoiDung;
            cbxNhomNguoiDung.DisplayMember = "TenNhom";
            cbxNhomNguoiDung.ValueMember = "IDNhomNguoiDung";
            //cbxNhomNguoiDung.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDNhomNguoiDung", true);

            cbxModule.DataSource = Cache.Context.ACL_Module;
            cbxModule.DisplayMember = "TenModule";
            cbxModule.ValueMember = "MaModule";
            cbxModule.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "MaModule", true);

            chbxSua.DataBindings.Add(Const.BIND_CHECKED, _bindingSource, "Sua", true);
            chbxThem.DataBindings.Add(Const.BIND_CHECKED, _bindingSource, "Them", true);
            chbxXoa.DataBindings.Add(Const.BIND_CHECKED, _bindingSource, "Xoa", true);

            colTenModule.DataSource = Cache.Context.ACL_Module;
            colTenModule.DisplayMember = "TenModule";
            colTenModule.ValueMember = "MaModule";

            //colMaModule.DataPropertyName = "IDModule";
            colMaModule.DataSource = Cache.Context.ACL_Module;
            colMaModule.DisplayMember = "MaModule";
            colMaModule.ValueMember = "MaModule";

            colTenNhom.DataSource = Cache.Context.ACL_NhomNguoiDung;
            colTenNhom.DisplayMember = "TenNhom";
            colTenNhom.ValueMember = "IDNhomNguoiDung";

            base.AddDataBinding();

        }

        protected override void AddNew()
        {
            try
            {
                if (cbxNhomNguoiDung.SelectedValue != null)
                {
                    int idNhom;

                    if (int.TryParse(cbxNhomNguoiDung.SelectedValue.ToString(), out idNhom))
                    {
                        //get module
                        List<ACL_Module> lstModule = Cache.Context.ACL_Module.ToList();
                        ACL_PhanQuyenNhomNguoiDung phanQuyen = null;

                        foreach (ACL_Module module in lstModule)
                        {
                            phanQuyen = Cache.Context.ACL_PhanQuyenNhomNguoiDung.Where(s => s.IDNhomNguoiDung == idNhom
                                && s.MaModule == module.MaModule).FirstOrDefault();

                            if (phanQuyen != null )
                            {
                                return;
                            }

                            phanQuyen = new ACL_PhanQuyenNhomNguoiDung
                            {
                                IDNhomNguoiDung = idNhom,
                                MaModule = module.MaModule,
                                NgayTao = DateTime.Now,
                                Them = true,
                                Xoa = true,
                                Sua = true,
                                Xem = true
                            };

                            this._bindingSource.Add(phanQuyen);

                            Cache.Context.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi");
            };
        }

        protected override void Delete()
        {
            try
            {
                base.Delete();
                Cache.Context.SaveChanges();
            }
            catch (Exception ex)
            {
                UICommon.ShowMessgeString(ex.ToString(), "Lỗi");
            }
        }

        #endregion

        #region Events

        private void dgvPhanQuyen_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
       
        private void chbx_CheckedChanged(object sender, DevComponents.DotNetBar.Controls.CheckBoxXChangeEventArgs e)
        {
            try            
            {
                Cache.Context.SaveChanges();
            }
            catch(Exception ex)
            {
                UICommon.ShowMessgeString("Gặp lỗi trong quá trình cập nhật: "+ex.Message, "Lỗi");
            }
        }

        private void cbxNhomNguoiDung_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {

                if (cbxNhomNguoiDung.SelectedValue != null)
                {
                    int idNhom;
                    if (int.TryParse(cbxNhomNguoiDung.SelectedValue.ToString(), out idNhom))
                    {
                        var data = Cache.Context.ACL_PhanQuyenNhomNguoiDung.Where(s => s.IDNhomNguoiDung == idNhom);
                        this._bindingSource.DataSource = data;
                        this.dgvPhanQuyen.DataSource = _bindingSource;
                    }
                }
            }
            catch (Exception ex)
            {
                UICommon.ShowMessgeString(ex.ToString(), "Lỗi");
            }
        }

        #endregion


    }
}
